package com.ace.lab.users.service;

import com.ace.lab.users.dto.UserQueryRequestProto;
import com.ace.lab.users.dto.UserQueryResponseProto;
import com.ace.lab.users.entity.LoginAccount;
import com.ace.lab.users.entity.User;
import com.ace.lab.users.mapper.LoginAccountMapper;
import com.ace.lab.users.mapper.UserMapper;
import io.grpc.stub.StreamObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase {

    @Autowired
    private LoginAccountMapper loginAccountMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void queryByUserName(UserQueryRequestProto.UserQueryRequest request, StreamObserver<UserQueryResponseProto.UserQueryResponse> responseObserver) {
        responseObserver.onNext(queryByUserName(request));
        responseObserver.onCompleted();
    }

    private UserQueryResponseProto.UserQueryResponse queryByUserName(UserQueryRequestProto.UserQueryRequest request) {
        LoginAccount loginAccount = loginAccountMapper.selectByUserName(request.getUserName());
        User user = userMapper.selectByPrimaryKey(loginAccount.getUserId());
        return UserQueryResponseProto.UserQueryResponse.newBuilder()
                .setId(user.getId())
                .setNickName(user.getNickName())
                .setUserName(loginAccount.getUserName())
                .setPassword(loginAccount.getPassword())
                .build();
    }

}
